package com.base.common.provider;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import com.base.commonKey.entity.CommonKey;
import com.base.commonKey.entity.CommonKeyType;
import com.base.commonKey.service.ICommonKeyService;
import com.base.commons.SpringBeanHolder;

public class CommonKeySelectItemProvider {
	/**
	 * 取某一类型的选择项,带有层级关系的也都出来
	 * 
	 * @param keyType
	 *            选择项的类型
	 * @param valueSelect
	 *            是否存值
	 * @return
	 */
	public static SelectItem[] getCommonKey(String keyType, boolean valueSelect) {
		if (keyType == null || "".equals(keyType))
			return null;
		ICommonKeyService ckService = SpringBeanHolder
				.getBean("commonKeyService");
		List<CommonKey> cks = ckService.getAll(keyType);
		List<SelectItem> temp = new ArrayList<SelectItem>();
		for (CommonKey ck : cks) {
			if (ck.isCheckEnable()) {
				if (valueSelect) {
					temp
							.add(new SelectItem(ck.getKeyValue(), ck
									.getKeyValue()));
				} else {
					temp.add(new SelectItem(ck.getId(), ck.getKeyValue()));
				}
			}
		}
		SelectItem[] result = new SelectItem[temp.size()];
		temp.toArray(result);
		return result;
	}

	/**
	 * 取某一类型的顶层的选择项
	 * 
	 * @param keyType
	 * @param valueSelect
	 * @return
	 */
	public static SelectItem[] getTopCommonKey(String keyType,
			boolean valueSelect) {
		if (keyType == null || "".equals(keyType))
			return null;
		ICommonKeyService ckService = SpringBeanHolder
				.getBean("commonKeyService");
		List<CommonKey> cks = ckService.getTopAll(keyType);
		List<SelectItem> temp = new ArrayList<SelectItem>();
		for (CommonKey ck : cks) {
			if (ck.isCheckEnable()) {
				if (valueSelect) {
					temp
							.add(new SelectItem(ck.getKeyValue(), ck
									.getKeyValue()));
				} else {
					temp.add(new SelectItem(ck.getId(), ck.getKeyValue()));
				}
			}
		}
		SelectItem[] result = new SelectItem[temp.size()];
		temp.toArray(result);
		return result;
	}

	/**
	 * 取某一类型的直接子集
	 * 
	 * @param parentCk
	 * @param valueSelect
	 * @return
	 */
	public static SelectItem[] getDirectCommonKey(String parentCk,
			boolean valueSelect) {
		if (parentCk == null || "".equals(parentCk))
			return null;
		ICommonKeyService ckService = SpringBeanHolder
				.getBean("commonKeyService");
		List<CommonKey> cks = ckService.getChilden(Integer.parseInt(parentCk));
		List<SelectItem> temp = new ArrayList<SelectItem>();
		for (CommonKey ck : cks) {
			if (ck.isCheckEnable()) {
				if (valueSelect) {
					temp
							.add(new SelectItem(ck.getKeyValue(), ck
									.getKeyValue()));
				} else {
					temp.add(new SelectItem(ck.getId(), ck.getKeyValue()));
				}
			}
		}
		SelectItem[] result = new SelectItem[temp.size()];
		temp.toArray(result);
		return result;
	}

	/**
	 * 取所有的选项类型
	 * 
	 * @param last
	 * @return
	 */
	public static SelectItem[] getCommonKeyType() {
		ICommonKeyService ckService = SpringBeanHolder
				.getBean("commonKeyService");
		List list = ckService.getAllCommonKeyType();
		if (list != null && list.size() > 0) {
			SelectItem[] cks = new SelectItem[list.size()];
			for (int i = 0; i < list.size(); i++) {
				CommonKeyType ck = (CommonKeyType) list.get(i);
				cks[i] = new SelectItem(ck.getTypeCode(), ck.getTypeName());
			}
			return cks;
		}
		return null;
	}
}
